package com.tjsoft.demo.student.action;

import java.util.List;

import com.tjsoft.demo.student.model.Student;
import com.tjsoft.demo.student.service.StudentService;

public class StudentAction extends BaseAction{

	private StudentService studentService;
	private Long sid;
	private String password;
	
	/**
	 * 
	 * @return
	 */
	public String studentList(){
		System.out.println("this is a first---------------------------------------------------");
		if(sid==null || "".equals(sid)){
			sid=(long) 1;
			Student student = studentService.findById(sid);
			System.out.println("-------------------------"+student.getName());
			setValidMessage(this.getText("info.login.sidIsNull"));
			return "login";
		}
		if(password==null || "".equals(password)){
			setValidMessage(this.getText("info.login.passwordIsNull"));
			return "login";
		}
		try{
			Student student = studentService.findById(sid);
			System.out.println(student+password+":"+sid);
			if(student.getPassword().equals(password)){
				this.getHttpSession().setAttribute("student", student);
				List<Student> list = studentService.findAll();
				request.setAttribute("list", list);
				return "studentList";
			}else{
				setValidMessage(this.getText("info.login.LoginFailed"));
				return "login";
			}
		}catch(Exception e){
			e.printStackTrace();
			setValidMessage(this.getText("info.login.LoginFailed"));
			return "login";
		}
	}	
	/**
	 * 删除学生
	 * @return
	 */
	public String delete(){
		try{
			String sid = request.getParameter("sid");
			Student entity = new Student();
			entity.setSid(Long.valueOf(sid));
			studentService.delete(entity);
		}catch(Exception e){
			e.printStackTrace();
		}
		List<Student> list = studentService.findAll();
		request.setAttribute("list", list);
		return "studentList";
	}
	
	public String addStudent(){
		return "addStudent";
	}
	
	public String saveStudent(){
		try{
			String sid = request.getParameter("sid");
			String password = request.getParameter("password");
			String name = request.getParameter("name");
			String age = request.getParameter("age");
			String sex = request.getParameter("sex");
			Student entity = new Student();
			entity.setSid(Long.valueOf(sid));
			entity.setPassword(password);
			entity.setName(name);
			entity.setAge(Integer.valueOf(age));
			entity.setSex(Integer.valueOf(sex));
			studentService.save(entity);
		}catch(Exception e){
			e.printStackTrace();
		}
		List<Student> list = studentService.findAll();
		request.setAttribute("list", list);
		return "studentList";
	}
	
	public String getStudent(){
		String sid = request.getParameter("sid");
		try{
			Student student = studentService.findById(Long.valueOf(sid));
			request.setAttribute("student", student);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "updateStudent";
	}	
	public String updateStudent(){
		try{
			String sid = request.getParameter("sid");
			String name = request.getParameter("name");
			String age = request.getParameter("age");
			String sex = request.getParameter("sex");
			Student entity = studentService.findById(Long.valueOf(sid));
			entity.setSid(Long.valueOf(sid));
			entity.setName(name);
			entity.setAge(Integer.valueOf(age));
			entity.setSex(Integer.valueOf(sex));
			studentService.update(entity);
		}catch(Exception e){
			e.printStackTrace();
		}
		List<Student> list = studentService.findAll();
		request.setAttribute("list", list);
		return "studentList";
	}
	
	public StudentService getStudentService() {
		return studentService;
	}

	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	public Long getSid() {
		return sid;
	}

	public void setSid(Long sid) {
		this.sid = sid;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	
}
